import axios from 'axios';
import {CookieUtil} from "@/utils/cookie";

// 创建 axios 实例
const service = axios.create({
    baseURL: 'http://localhost:3000/api', // 若有基础 URL 可在此设置
    timeout: 5000 // 请求超时时间
});

// 请求拦截器
service.interceptors.request.use(
    config => {
        if (CookieUtil.getCookie("token")) {
            config.headers["Authorization"] = CookieUtil.getCookie("token");
        }
        return config;
    },
    error => {
        console.log(error);
        Promise.reject(error);
    }
);

// 响应拦截器
service.interceptors.response.use(
    response => {
        return response.data;
    },
    error => {
        console.log('err' + error,error.msg);
        return Promise.reject(error);
    }
);

export default service;
